package admin

import (
	"chaowan/data_source"
	"chaowan/models"
	"chaowan/service"
	"github.com/gin-gonic/gin"
	"net/http"
	"strings"
	"time"
)

type FanshangcjcslistRBody struct {
	Data       []models.Orderfanshanggoodcs `json:"data"`
	PageNo     int                          `json:"pageNo"`
	PageSize   int                          `json:"pageSize"`
	TotalCount int64                        `json:"totalCount"`
	TotalPage  int64                        `json:"totalPage"`
}

type FanshangcjcslistBody struct {
	Page       int   `json:"page"`
	Pagesize   int   `json:"pagesize"`
	Classify   int64 `json:"classify" gorm:"classify"`
	Fanshangid int64 `json:"fanshangid" gorm:"fanshangid"`
	Number     int64 `json:"number" gorm:"number"`
	State      int64 `json:"state" gorm:"state"`
}

func Fanshangcjcs(ctx *gin.Context) {
	request := FanshangcjcslistBody{}
	ctx.ShouldBind(&request)
	fanshangid := request.Fanshangid
	number := request.Number
	data_source.Db.Where("fanshangid = ?", fanshangid).Delete(&models.Orderfanshanggoodcs{})

	var integral int64
	var zhuanpan models.Zhuanpan
	data_source.Db.Where("fanshangid = ?", fanshangid).Where("state = 1").First(&zhuanpan)
	var fanshang models.Fanshang
	data_source.Db.Where("id = ?", fanshangid).First(&fanshang)

	var fanshanggoods []models.Fanshanggood
	var shangjuan models.Shangjuan
	if zhuanpan.ID > 0 {
		dengji := strings.Split(zhuanpan.Dengji, ",")
		if len(dengji) == 0 {
			data_source.Db.Where("fanshangid = ?", fanshangid).Find(&fanshanggoods)
		} else {
			data_source.Db.Where("fanshangid = ?", fanshangid).Where("classify not in (?)", dengji).Find(&fanshanggoods)
		}
		if zhuanpan.Classify == 1 {
		} else {
			data_source.Db.Where("id = ?", zhuanpan.Shangjuanid).First(&shangjuan)
		}
	} else {
		data_source.Db.Where("fanshangid = ?", fanshangid).Find(&fanshanggoods)
	}

	var anum int64
	var bnum int64
	var cnum int64
	var dnum int64
	for _, f := range fanshanggoods {
		if f.Classify == 1 {
			anum = anum + f.Num
		}
		if f.Classify == 2 {
			bnum = bnum + f.Num
		}
		if f.Classify == 3 {
			cnum = cnum + f.Num
		}
		if f.Classify == 4 {
			dnum = dnum + f.Num
		}
	}
	var abili float64
	var bbili float64
	var cbili float64
	var dbili float64

	var goodidarr []int64
	for k, v := range fanshanggoods {
		if v.Classify == 1 {
			abili = float64(v.Num) / float64(anum)
			fanshanggoods[k].Bili = fanshang.Ashang * 100 * abili
		} else if v.Classify == 2 {
			bbili = float64(v.Num) / float64(bnum)
			fanshanggoods[k].Bili = fanshang.Bshang * 100 * bbili
		} else if v.Classify == 3 {
			cbili = float64(v.Num) / float64(cnum)
			fanshanggoods[k].Bili = fanshang.Cshang * 100 * cbili
		} else if v.Classify == 4 {
			dbili = float64(v.Num) / float64(dnum)
			fanshanggoods[k].Bili = fanshang.Dshang * 100 * dbili
		}
		goodidarr = append(goodidarr, v.Goodid)
	}

	var goods []models.Goods
	data_source.Db.Where("id in (?)", goodidarr).Find(&goods)

	now := time.Now()
	zeroToday := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Unix()

	var ordergoods []models.Orderfanshanggoodcs
	for i := 0; i < int(number); i++ {
		goodid, dengji := service.GetResultfs(fanshanggoods)

		for _, good := range goods {
			if goodid == good.ID {
				orderniudangood := models.Orderfanshanggoodcs{
					Goodid:     goodid,
					Goodname:   good.Name,
					Goodimage:  good.Image,
					Goodmojing: good.Mojing,
					Goodprice:  good.Price,
					Fanshangid: fanshangid,
					Addtime:    zeroToday,
					Num:        1,
					Dengji:     dengji,
					State:      1,
				}
				ordergoods = append(ordergoods, orderniudangood)
			}
		}
		if zhuanpan.ID > 0 {
			zhongjian := service.CalculateProbability(int64(zhuanpan.Bili * 100))
			if zhongjian == true {
				if zhuanpan.Classify == 1 {
					integral = integral + zhuanpan.Integral*zhuanpan.Num
				} else {
					var sjnum int64
					for iu := 0; iu < int(zhuanpan.Num); iu++ {
						sjnum = sjnum + shangjuan.Num
					}
					orderniudangsj := models.Orderfanshanggoodcs{
						Goodname:   "赏券",
						Num:        sjnum,
						Goodimage:  "http://interstellar-tide-play.oss-cn-shanghai.aliyuncs.com/shequ/2024-10-27/1730020843赏券@3x.png",
						State:      3,
						Fanshangid: fanshangid,
					}
					ordergoods = append(ordergoods, orderniudangsj)
				}
			}
		}
	}

	if integral > 0 {
		orderniudangjifen := models.Orderfanshanggoodcs{
			Goodname:   "积分",
			Num:        integral,
			Goodimage:  "http://interstellar-tide-play.oss-cn-shanghai.aliyuncs.com/shequ/2024-10-27/1730020800积分@3x.png",
			State:      2,
			Fanshangid: fanshangid,
		}
		ordergoods = append(ordergoods, orderniudangjifen)

	}
	data_source.Db.Create(&ordergoods)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Fanshangcjcslist(ctx *gin.Context) {
	request := FanshangcjcslistBody{}
	ctx.ShouldBind(&request)
	classify := request.Classify
	currentPage := request.Page
	pageSize := request.Pagesize
	fanshangid := request.Fanshangid
	state := request.State
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Orderfanshanggoodcs
	res := data_source.Db.Where("fanshangid = ?", fanshangid)
	if classify > 0 {
		res = res.Where("classify = ?", classify)
	}
	if state > 0 {
		res = res.Where("state = ?", state)
	}
	res.Model(&models.Orderfanshanggoodcs{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := FanshangcjcslistRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

type NiudancjcslistRBody struct {
	Data       []models.Orderniudangoodcs `json:"data"`
	PageNo     int                        `json:"pageNo"`
	PageSize   int                        `json:"pageSize"`
	TotalCount int64                      `json:"totalCount"`
	TotalPage  int64                      `json:"totalPage"`
}

type NiudancjcslistBody struct {
	Page     int   `json:"page"`
	Pagesize int   `json:"pagesize"`
	Classify int64 `json:"classify" gorm:"classify"`
	Niudanid int64 `json:"niudanid" gorm:"niudanid"`
	Number   int64 `json:"number" gorm:"number"`
	State    int64 `json:"state" gorm:"state"`
}

func Niudancjcs(ctx *gin.Context) {
	request := NiudancjcslistBody{}
	ctx.ShouldBind(&request)
	niudanid := request.Niudanid
	number := request.Number
	data_source.Db.Where("niudanid = ?", niudanid).Delete(&models.Orderniudangoodcs{})

	var integral int64
	var zhuanpan models.Zhuanpan
	data_source.Db.Where("niudanid = ?", niudanid).Where("state = 1").First(&zhuanpan)
	var niudan models.Niudan
	data_source.Db.Where("id = ?", niudanid).First(&niudan)

	var niudangoods []models.Niudangood
	var shangjuan models.Shangjuan
	if zhuanpan.ID > 0 {
		dengji := strings.Split(zhuanpan.Dengji, ",")
		if len(dengji) == 0 {
			data_source.Db.Where("niudanid = ?", niudanid).Find(&niudangoods)
		} else {
			data_source.Db.Where("niudanid = ?", niudanid).Where("classify not in (?)", dengji).Find(&niudangoods)
		}
		if zhuanpan.Classify == 1 {
		} else {
			data_source.Db.Where("id = ?", zhuanpan.Shangjuanid).First(&shangjuan)
		}
	} else {
		data_source.Db.Where("niudanid = ?", niudanid).Find(&niudangoods)
	}

	var anum int64
	var bnum int64
	var cnum int64
	var dnum int64
	for _, f := range niudangoods {
		if f.Classify == 1 {
			anum = anum + f.Num
		}
		if f.Classify == 2 {
			bnum = bnum + f.Num
		}
		if f.Classify == 3 {
			cnum = cnum + f.Num
		}
		if f.Classify == 4 {
			dnum = dnum + f.Num
		}
	}
	var abili float64
	var bbili float64
	var cbili float64
	var dbili float64

	var goodidarr []int64
	for k, v := range niudangoods {
		if v.Classify == 1 {
			abili = float64(v.Num) / float64(anum)
			niudangoods[k].Bili = niudan.Chuanshuo * 100 * abili
		} else if v.Classify == 2 {
			bbili = float64(v.Num) / float64(bnum)
			niudangoods[k].Bili = niudan.Shishi * 100 * bbili
		} else if v.Classify == 3 {
			cbili = float64(v.Num) / float64(cnum)
			niudangoods[k].Bili = niudan.Putong * 100 * cbili
		} else if v.Classify == 4 {
			dbili = float64(v.Num) / float64(dnum)
			niudangoods[k].Bili = niudan.Gaoji * 100 * dbili
		}
		goodidarr = append(goodidarr, v.Goodid)
	}
	var goods []models.Goods
	data_source.Db.Where("id in (?)", goodidarr).Find(&goods)

	now := time.Now()
	zeroToday := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Unix()

	var orderniudangoods []models.Orderniudangoodcs
	for i := 0; i < int(number); i++ {
		goodid, dengji := service.GetResult(niudangoods)
		for _, good := range goods {
			if goodid == good.ID {
				orderniudangood := models.Orderniudangoodcs{
					Goodid:     goodid,
					Goodname:   good.Name,
					Goodimage:  good.Image,
					Goodmojing: good.Mojing,
					Goodprice:  good.Price,
					Niudanid:   niudanid,
					Addtime:    zeroToday,
					Num:        1,
					Dengji:     dengji,
					State:      1,
				}
				orderniudangoods = append(orderniudangoods, orderniudangood)
			}
		}
		if zhuanpan.ID > 0 {
			zhongjian := service.CalculateProbability(int64(zhuanpan.Bili * 100))
			if zhongjian == true {
				if zhuanpan.Classify == 1 {
					integral = integral + zhuanpan.Integral*zhuanpan.Num
				} else {
					var sjnum int64
					for iu := 0; iu < int(zhuanpan.Num); iu++ {
						sjnum = sjnum + shangjuan.Num
					}
					orderniudangsj := models.Orderniudangoodcs{
						Goodname:  "赏券",
						Num:       sjnum,
						Goodimage: "http://interstellar-tide-play.oss-cn-shanghai.aliyuncs.com/shequ/2024-10-27/1730020843赏券@3x.png",
						State:     3,
						Niudanid:  niudanid,
					}
					orderniudangoods = append(orderniudangoods, orderniudangsj)
				}
			}
		}
	}

	if integral > 0 {
		orderniudangjifen := models.Orderniudangoodcs{
			Goodname:  "积分",
			Num:       integral,
			Goodimage: "http://interstellar-tide-play.oss-cn-shanghai.aliyuncs.com/shequ/2024-10-27/1730020800积分@3x.png",
			State:     2,
			Niudanid:  niudanid,
		}
		orderniudangoods = append(orderniudangoods, orderniudangjifen)

	}
	data_source.Db.Create(&orderniudangoods)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Niudancjcslist(ctx *gin.Context) {
	request := NiudancjcslistBody{}
	ctx.ShouldBind(&request)
	classify := request.Classify
	currentPage := request.Page
	pageSize := request.Pagesize
	niudanid := request.Niudanid
	state := request.State
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Orderniudangoodcs
	res := data_source.Db.Where("niudanid = ?", niudanid)
	if classify > 0 {
		res = res.Where("classify = ?", classify)
	}
	if state > 0 {
		res = res.Where("state = ?", state)
	}
	res.Model(&models.Orderniudangoodcs{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := NiudancjcslistRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}
